<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100px" align="center"></el-table-column>
      <el-table-column prop="avatar" label="头像" width="100px" align="center">
        <template slot-scope="scope">
          <el-avatar :size="40" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" header-align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" header-align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" header-align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="description" label="简介" width="200px" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="是否启用" width="80" header-align="center">
        <template slot-scope="scope">
        <el-switch
            v-model="scope.row.enable"
            @change="toggleEnable(scope.row)"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#cccccc">
        </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
                     circle>编辑
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"
                     @click="confirmDelete(scope.row)" circle>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  弹出编辑管理员的弹框  -->
    <el-dialog title="修改管理员" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input style="width: 800px" v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input style="width: 800px"  v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item style="width: 800px" label="排序" :label-width="formLabelWidth">
          <el-input style="width: 800px" v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id:'',
        name: '',
        description:'',
        sort:''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {

    //执行启用或禁用
    toggleEnable(admin){
      let enableText = ['禁用','启用'];
      let url = 'http://localhost:9080/admin/'+admin.id+'/';
      if (admin.enable == 1){
        url += 'enable';
      }else {
        url += 'disable';
      }
      console.log('url=' + url);

      let jwt = localStorage.getItem('jwt');
      console.log('local storage jwt = ' + jwt);

      this.axios
          .create({headers: {'Authorization' : jwt}})
          .post(url).then((response) =>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          let message = '操作成功，已经将【' + admin.name
          + '】的状态改为【' + enableText[admin.enable] + '】！';
          this.$message.success(message);
        }else {
          this.$message.error(responseBody.message);
          this.loadAdminList();
        }
      })
    },

    //提交修改
    /*submitEditForm(){
      let formData = this.qs.stringify(this.editForm);
      console.log(formData);
      let url = 'http://localhost:8080/albums/'+this.editForm.id+'/update';
      this.axios.post(url,formData).then((response) =>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message.success("修改相册信息成功！");
          this.dialogFormVisible = false;
          this.loadAlbumList();
        }else if (responseBody.state == 40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },*/

    /*//弹出编辑框
    openEditDialog(album) {
      console.log('尝试打开编辑[id=' + album.id + ']数据的对话框');

      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get('http://localhost:8080/albums/'+album.id+'/select').then((response) =>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        }else{
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }
      })
    },*/

    //弹出确认删除框
    confirmDelete(admin) {
      console.log('尝试打开删除[id=' + admin.id + ']数据的对话框');
      let message = '此操作将永久删除' + admin.username + '管理员, 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {
      });
    },

    //执行删除
    handleDelete(admin) {
     this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post('http://localhost:9080/admin/' + admin.id + '/delete').then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message.success('删除成功！');
          this.loadAdminList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },

    //列表展示
    loadAdminList() {
      let jwt = localStorage.getItem('jwt');
      console.log('local storage jwt = ' + jwt);

      let url = 'http://localhost:9080/admin/list';
      console.log('url = '+ url);

      this.axios
          .create({'headers':{'Authorization': jwt}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          console.log("进来了");
          this.tableData = responseBody.data;
        }
      });
    }
  },
  mounted() {
    this.loadAdminList();
  }
}
</script>

<style>

</style>